
package com.intretech.umsin.system.controller;

import com.intretech.umsin.common.web.ApiResult;
import org.springframework.util.CollectionUtils;
import com.intretech.umsin.system.vo.SysMenuVo;
import com.intretech.umsin.system.entity.SysMenuEntity;
import com.intretech.umsin.system.service.SysMenuService;
import com.intretech.umsin.system.param.SysMenuParam;
import com.intretech.umsin.common.excel.service.ExcelService;
import org.apache.commons.io.IOUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;

import java.util.UUID;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
* 菜单权限:sys_menu 对应的API访问类.
* @ClassName: SysMenuController
* @Description: 菜单权限:sys_menu 对应的API访问类.
* @author: 李启联
* @date:   2022-8-8 8:02:22
* @version V1.0
*/
@RestController
@RequestMapping("/system/sysmenu")
@Api(tags = "菜单权限 API")
public class SysMenuController {


    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ExcelService excelService;

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询单个菜单权限对象", notes = "查询单个菜单权限对象")
    public ApiResult<SysMenuVo> querySysMenuById(@PathVariable("id") String id){
        Function<String,SysMenuVo> fun = p -> sysMenuService.queryById(p);
        return ApiResult.responseFor(fun,id);
    }

    @PostMapping("/listByIds")
    @ApiOperation(value = "根据ID列表查询菜单权限集合对象", notes = "根据ID列表查询菜单权限集合对象")
    public ApiResult<List<SysMenuVo>> querySysMenuByIds(@RequestBody List<String> ids){
        Function<List<String>,List<SysMenuVo>> fun = p -> sysMenuService.queryByIds(p);
        return ApiResult.responseFor(fun,ids);
    }

    @PostMapping("/page/{pageNo}/{pageSize}")
    @ApiOperation(value = "查询返回菜单权限分页对象", notes = "查询返回菜单权限分页对象")
    public ApiResult<IPage<SysMenuVo>> queryForPage(@RequestBody SysMenuParam params, @PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize){
        BiFunction<IPage,SysMenuParam,IPage<SysMenuVo>> fun = (p1, p2) -> sysMenuService.queryForPage(p1, p2);
        return ApiResult.responseFor(fun,new Page(pageNo,pageSize), params);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询菜单权限对象列表", notes = "查询返回菜单权限对象列表")
    public ApiResult<List<SysMenuVo>> queryForList(@RequestBody SysMenuParam params){
        Function<SysMenuParam,List<SysMenuVo>> fun = p -> sysMenuService.queryForList(p);
        return ApiResult.responseFor(fun,params);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增菜单权限对象", notes = "新增菜单权限对象")
    public ApiResult<SysMenuVo> add(@RequestBody SysMenuVo vo){
        Function<SysMenuVo,SysMenuVo> fun = p -> sysMenuService.add(p);
        return ApiResult.responseFor(fun,vo);
    }

    @PostMapping("/adds")
    @ApiOperation(value = "新增菜单权限对象列表", notes = "新增菜单权限对象列表")
    public ApiResult<List<SysMenuVo>> adds(@RequestBody List<SysMenuVo> voList){
        Function<List<SysMenuVo>,List<SysMenuVo>> fun = p -> sysMenuService.adds(p);
        return ApiResult.responseFor(fun,voList);
    }

    @PutMapping("/modify")
    @ApiOperation(value = "更新菜单权限对象", notes = "更新菜单权限对象")
    public ApiResult<SysMenuVo> modify(@RequestBody SysMenuVo vo){
        Function<SysMenuVo,SysMenuVo> fun = p -> sysMenuService.modify(p);
        return ApiResult.responseFor(fun,vo);
    }

    @PutMapping("/modifys")
    @ApiOperation(value = "更新菜单权限对象", notes = "更新菜单权限对象")
    public ApiResult<List<SysMenuVo>> modifys(@RequestBody List<SysMenuVo> voList){
        Function<List<SysMenuVo>,List<SysMenuVo>> fun = p-> sysMenuService.modifys(p);
        return ApiResult.responseFor(fun,voList);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除单个菜单权限对象", notes = "删除单个菜单权限对象")
    public ApiResult<Void> removeUser(@PathVariable("id") String id){
        Consumer<String> consumer = p -> sysMenuService.removeById(p);
        return ApiResult.responseFor(consumer,id);
    }

    @DeleteMapping("/removes")
    @ApiOperation(value = "批量删除菜单权限对象", notes = "批量删除菜单权限对象")
    public ApiResult<Void> removeUsers(@RequestBody List<String> ids){
        Consumer<List<String>> consumer = p -> sysMenuService.removeBatchByIds(p);
        return ApiResult.responseFor(consumer, ids);
    }

   @PostMapping("/export")
   @ApiOperation(value = "菜单权限对象导出Excel", notes = "菜单权限对象导出Excel")
   public ApiResult<Void> exportExcel(@RequestBody SysMenuParam params , HttpServletResponse response) throws Exception {
       Consumer<SysMenuParam> consumer = p ->{
           try {
               List<SysMenuVo> ls = sysMenuService.queryForList(p);
               if(!CollectionUtils.isEmpty(ls)){
                   byte[] data = excelService.exportExcel(ls, SysMenuVo.class,"菜单权限");
                   response.reset();
                   //设置响应头
                   response.setHeader("Content-Disposition", "attachment; filename=\""+ UUID.randomUUID().toString().replace("-","").toUpperCase() +".xls\"");
                   response.addHeader("Content-Length", "" + data.length);
                   response.setContentType("application/msexcel; charset=UTF-8");
                   IOUtils.write(data, response.getOutputStream());
               }
           }catch (Exception e){
                throw new RuntimeException("export excel failed");
           }
       };
       return ApiResult.responseFor(consumer, params);
   }

}